<?php
/**
 * @author tungck
 * @filesource event.php
 * quan ly sự kiện 
*/

class Event extends CI_Controller{
    //public $data    =       array();
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        
        $userSession = $this->session->userdata('user');
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }else{
            if($userSession['useradmin'] != 1){
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
                
    }
    
    function list_event(){
        $title = "Các Sự Kiện";
        $this->adminlayout->view("admin/list_event",null ,$title);        
    }
    
    function getListEvent(){
        try{
            $data['sort'] = $_GET["jtSorting"];
            $data['startIndex'] = $_GET["jtStartIndex"];
            $data['pageSize'] = $_GET["jtPageSize"];
            $this->load->model("admin/event_model");
            $result = $this->event_model->getList($data);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            $jTableResult['TotalRecordCount'] = $result['recordCount'];
            $jTableResult['Records'] = $result['query'];
            print json_encode($jTableResult);
        }catch(Exception $ex){
            $jTableResult['Result'] = "ERROR";
            $jTableResult['Records'] = 'afhkahfkafhkahfakfhka';
            print json_encode($jTableResult);
        }
    }
    
    
    function editEvent(){
        if(isset($_GET["eventId"])){
            $id = $_GET["eventId"];
            $this->load->model("admin/event_model");
            $result = $this->event_model->getDetailList($id);
            $data['data'] = $result[0];
            $title = $result[0]->TieuDe;
        }else{
            $title = "Thêm sự kiện";
        }
        $this->load->helper('ckeditor');
        $data['ckeditor'] = array(
 
                        //ID of the textarea that will be replaced
                        'id'    =>      'eventContent',
                        'path'  =>      'ckeditor',
 
                        //Optionnal values
                        'config' => array(
                                'toolbar'       =>      "Full",         //Using the Full toolbar
                                'width'         =>      "95%",        //Setting a custom width
                                'height'        =>      '500px',        //Setting a custom height
 
                        ),
 
                        //Replacing styles from the "Styles tool"
                        'styles' => array(
 
                                //Creating a new style named "style 1"
                                'style 1' => array (
                                        'name'          =>      'Blue Title',
                                        'element'       =>      'h2',
                                        'styles' => array(
                                                'color'         =>      'Blue',
                                                'font-weight'   =>      'bold'
                                        )
                                ),
 
                                //Creating a new style named "style 2"
                                'style 2' => array (
                                        'name'  =>      'Red Title',
                                        'element'       =>      'h2',
                                        'styles' => array(
                                                'color'                 =>      'Red',
                                                'font-weight'           =>      'bold',
                                                'text-decoration'       =>      'underline'
                                        )
                                )                              
                        )
                );
        
        $this->adminlayout->view("admin/editDetail",$data ,$title);
        
    }
    
    function saveDb(){
        
        $data['TieuDe'] = $_POST['title'];
        $data['MoTa'] = $_POST["summary"];
        $data['NoiDung'] = $_POST["content"];
        $data['Ngay'] = date('Y-m-d');
        $this->load->model("admin/event_model");
        $result = $this->event_model->saveEvent($data);
        redirect(base_url('admin/event/list_event'));
		
    }
    
    function updateEvent(){
        $data['Ngay'] = date('Y-m-d');
        $data['TieuDe'] = $_POST['title'];
        $data['MoTa'] = $_POST["summary"];
        $data['NoiDung'] = $_POST["content"];
        $this->load->model("admin/event_model");
        if(isset($_POST['id'])){
            $data['MaSuKien'] = $_POST['id'];
            $result = $this->event_model->updateEvent($data);
        }else{
            $result = $this->event_model->saveEvent($data);
        }
        
        redirect(base_url('admin/event/list_event'));
		
    }
    
   function deleteEvent(){
        $id = $_POST['MaSuKien'];
        $this->load->model("admin/event_model");
        $result = $this->event_model->deleteEvent($id);
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        print json_encode($jTableResult);
        
        
        
   }
}

?>